package cn.tedu.cqhotel.controller;


import cn.tedu.cqhotel.pojo.dto.UserAddNewDTO;
import cn.tedu.cqhotel.pojo.dto.UserUpdateDTO;
import cn.tedu.cqhotel.pojo.vo.UserListItemVO;
import cn.tedu.cqhotel.pojo.vo.UserStandardVO;
import cn.tedu.cqhotel.service.IUserService;
import cn.tedu.cqhotel.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理用户信息相关请求的控制器
 *
 * @author yuliang
 */
@Slf4j
@RestController
@RequestMapping("/users")
@Api(tags = "07. 用户列表管理模块")
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    public UserController() {
        log.info("创建控制器：UserController");
    }

    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(@Validated UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加员工】的请求：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户管理id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【删除用户】的请求：id={}", id);
        userService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("修改用户信息")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户管理id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户信息】的请求：id={}, userUpdateDTO={}", id, userUpdateDTO);
        userService.updateById(id, userUpdateDTO);
        return JsonResult.ok();
    }


    @ApiOperation("根据用户id查询用户信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "userid", value = "用户userid", required = true, dataType = "long")
    @GetMapping("/{userid:[0-9]+}")
    public JsonResult<UserStandardVO> getStandardById(@PathVariable Long userid) {
        log.debug("开始处理【根据id查询员工信息】的请求：id={}", userid);
        UserStandardVO userStandardVO = userService.getStandardById(userid);
        return JsonResult.ok(userStandardVO);
    }

    @ApiOperation("根据name查询用户信息")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "name", value = "用户name", required = true, dataType = "string")
    @GetMapping("/name")
    public JsonResult<List<UserListItemVO>> listByName(String name) {
        log.debug("开始处理【根据name查询用户信息】的请求：name={}", name);
         List<UserListItemVO> list = userService.listByName(name);
        return JsonResult.ok(list);
    }

    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 400)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list() {
        log.debug("开始处理【查询员工列表】的请求……");
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

}
